<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Asubscribers extends Controller_Acommon {

	public $template = 'alayout/amain';

    public function params() {
        return array(
            'waymark' => 'email',
            'fields' => array(
                'name' => array(
                    'title' => 'Имя подписчика',
                    'type' => 'text',
                    'name' => 'name',
                ),
                'email' => array(
                    'title' => 'email',
                    'type' => 'text',
                    'name' => 'email',
                ),
                'type' => array(
                    'title' => 'Тип подписчика',
                    'type' => 'select',
                    'name' => 'type',
                    'select_params' => array(
                        'values' => array('1' => 'Юридическое лицо', '2' => 'Физическое лицо'),
                    )
                ),
            )
        );
    }

    public function showMailerForm() {
        $actionLink = '/administration/'.$this->request->controller().'/'.$this->request->param('page').'/send';

        $this->template->right .= View::factory('aforms/amailerForm', array(
            'actionLink' => $actionLink,
        ));
    }

    public function action_send() {
        $config = Kohana::$config->load('email');
        Email::connect($config);

        if ($this->request->post('type') == 0) {
            $to = ORM::factory($this->request->controller())->find_all()->as_array(NULL, 'email');
        } else {
            $to = ORM::factory($this->request->controller())->where('type', '=', $this->request->post('type'))->find_all()->as_array(NULL, 'email');
        }
        
        array_unshift($to, 'self@bell.kz');
        unset($to[0]);
        $from = 'noreply@bell.kz';
        $subject = $this->request->post('subject');
        $message = $this->request->post('content');

        Email::send($to, $from, $subject, $message, $html = true);
        $this->request->redirect('administration/'.$this->request->controller().'/'.$this->request->param('page'));
    }

	public function action_index() {
        $params = $this->params();
		$this->showDefaultList(); // Рендерим список данных и форму редактирования\добавления
        $this->showPagination(); // Пагинация
        $this->showDefaultForm($params, ORM::factory($this->request->controller()));
        $this->showMailerForm();
	}

    public function action_add() {
        $newObj = ORM::factory($this->request->controller());
        $newObj->name = $this->request->post('name');
        $newObj->email = $this->request->post('email');
        $newObj->type = $this->request->post('type');
        $newObj->save();
        $this->request->redirect('administration/'.$this->request->controller().'/'.$this->request->param('page'));
    }

    public function action_edit() {
        $obj = ORM::factory($this->request->controller(), $this->request->param('id'));

        if ($this->request->param('save')) {
            $obj->name = $this->request->post('name');
            $obj->email = $this->request->post('email');
            $obj->type = $this->request->post('type');
            $obj->save();
            $this->request->redirect('administration/'.$this->request->controller().'/'.$this->request->param('page').'/edit/'.$this->request->param('id'));
            exit();
        }

        $params = $this->params($obj);
        $this->showDefaultList(); // Рендерим список данных и форму редактирования\добавления
        $this->showPagination(); // Пагинация
        $this->showDefaultForm($params, $obj, 'edit');
        $this->showMailerForm();
    }

    public function action_delete() {
        $this->obj = ORM::factory($this->request->controller(), $this->request->param('id'));
        $this->obj->delete();
        $this->request->redirect('administration/'.$this->request->controller().'/'.$this->request->param('page').'/edit/'.$this->request->param('id'));
    }
}